package org.dtrd.modules.quartz.job;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.common.util.DateUtils;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.achievement.entity.po.DtrdEntRdSigninRecordSummary;
import org.dtrd.modules.achievement.service.IDtrdEntRdSigninRecordSummaryService;
import org.dtrd.modules.record.entity.po.DtrdEntRdSigninSugarValue;
import org.dtrd.modules.record.entity.po.DtrdViewRlCheckinRecord;
import org.dtrd.modules.record.service.IBloodPressureRecordService;
import org.dtrd.modules.record.service.IDtrdViewRlCheckinRecordService;
import org.dtrd.modules.record.service.IHeightWeightRecordService;
import org.dtrd.modules.record.service.ISugarRecordService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.StringJoiner;

/**
 * @author 萧
 * @date:2021-12-06 9:50 下午
 **/
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class WeekSummaryJob implements Job {

    @Autowired
    private ISugarRecordService sugarRecordService;
    @Autowired
    private IBloodPressureRecordService bloodPressureRecordService;
    @Autowired
    private IHeightWeightRecordService heightWeightRecordService;
    @Autowired
    private IDtrdEntRdSigninRecordSummaryService recordSummaryService;
    @Autowired
    private IDtrdViewRlCheckinRecordService checkinRecordService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        Date today = new Date();
        String todayStr = DateUtils.formatDate(today);
        DateTime weekDateTime = DateUtil.beginOfWeek(today);
        String startOfWeek = DateUtils.formatDate(weekDateTime);
        DateTime monthDateTime = DateUtil.beginOfMonth(today);
        String startOfMonth = DateUtils.formatDate(monthDateTime);
        log.info("WeekSummaryJob.execute.[context] today = " + today + " ; weekDateTime=" + weekDateTime + "； startOfWeek=" + startOfWeek + "; monthDateTime=" + monthDateTime + "; startOfMonth=" + startOfMonth);
        List<DtrdEntRdSigninRecordSummary> thisWeekSummary = recordSummaryService.getThisWeekSummary(startOfWeek);
        log.info("WeekSummaryJob.execute.[context] thisWeekSummary=" + thisWeekSummary);
        List<DtrdEntRdSigninRecordSummary> lastWeekSummary = recordSummaryService.getLastWeekSummary(startOfWeek);
        log.info("WeekSummaryJob.execute.[context] lastWeekSummary=" + lastWeekSummary);
        // 查询本周血糖、血压、身高体重打卡记录
        StringJoiner sj = new StringJoiner(",", "(", ")");
        sj.add(Constant.RecordType.SUGAR.getType() + "").add(Constant.RecordType.WEIGHT.getType() + "").add(Constant.RecordType.BLOOD_PRESSURE.getType() + "");
        List<DtrdViewRlCheckinRecord> checkinRecords = checkinRecordService.selectCheckinRecordsByRecordTypes(null, startOfWeek, null, sj.toString());

    }
}
